package com.example.springdocdemo.rest;

import com.example.springdocdemo.dto.BookUploadDto;
import com.example.springdocdemo.util.ResultHelper;
import com.example.springdocdemo.vo.BookVO;
import com.example.springdocdemo.vo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author susc
 * @title: SwaggerTestController
 * @projectName knife4j-demo
 * @description:
 *
 * @date 2021/9/299:43
 */
@RestController
@RequestMapping("book")
@Tag(name = "book接口", description = "book相关接口")
@Slf4j
public class SwaggerTestController {

    @GetMapping("/info")
    @Operation(summary="获取info接口", description = "获取info接口")
    public CommonResult<String> getBookInfo(String id, String name){
        log.info("id:" + id);
        log.info("name:" + name);
        return ResultHelper.success("成功");
    }

    @GetMapping("/detail")
    @Operation(summary = "根据id获取book详情接口")
    public CommonResult<BookVO> getBookInfo2(@Parameter(name = "bookid",required = true) String id){
        BookVO bookVO = new BookVO();
        bookVO.setId(id);
        bookVO.setName("book测试名称");
        return ResultHelper.success("成功", bookVO);
    }

    @DeleteMapping("delete")
    @Operation(summary = "根据id删除book接口")
    public CommonResult<String> deleteBook(@Parameter(name = "bookid",required = true)String id){
        return ResultHelper.success("成功");
    }

    @PostMapping(value = "/upload",consumes = "multipart/form-data", headers = "content-type=multipart/form-data")
    @Operation(summary = "上传book附件接口",description = "上传附件方法1")
    public CommonResult<BookVO> uploadBook(MultipartFile file){
        if(null == file){
            throw new NullPointerException("file is null!");
        }
        BookVO bookVO = new BookVO();
        bookVO.setName("测试上传");
        return ResultHelper.success("成功", bookVO);
    }

    @PostMapping(value = "/upload2",consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @Operation(summary = "上传book附件接口2", description = "上传附件方法2")
    public CommonResult<BookVO> uploadBook2(BookUploadDto bookUploadDto){
        MultipartFile file = bookUploadDto.getFile();
        if(null == file){
            throw new NullPointerException("file is null!");
        }
        BookVO bookVO = new BookVO();
        bookVO.setName("测试上传");
        return ResultHelper.success("成功", bookVO);
    }


}
